package com.VolunteerPlatform.Controller.volunteer;

import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Entity.VolunteerSign;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerInfoService;
import com.VolunteerPlatform.Service.VolunteerService.VolunteerSignService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by DoveXL on 2018/12/15.
 */
@Controller
@RequestMapping(value = "/VolunteerSign/**")
public class VolunteerSignController {
    @Autowired
    private VolunteerSignService volunteerSignService;

    @Autowired
    private VolunteerInfoService volunteerInfoService;



    @RequestMapping(value = "checkVolunteerSignInfo", method = RequestMethod.POST)
    @ResponseBody
    public String checkVolunteerSign(@RequestBody String jsondata, HttpSession httpSession)
    {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String DateDay = jsonObject.getString("Date");
        List<VolunteerSign> volunteerSignList = volunteerSignService.getAllVolunteerSign(IDCard,Name,null,null,DateDay,-1,null);
        if(volunteerSignList.size()==0)
        {
            return "1";
        }else
        {
            return "0";
        }
    }


    @RequestMapping(value = "AddVolunteerSignInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String AddVolunteerSign(@RequestBody String jsondata, HttpSession httpSession)
    {
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String DateDay = jsonObject.getString("Date");
        String Time = jsonObject.getString("Time");
        int isFullDay =0;
        if(Time.equals("9:00-11:30"))
        {
            isFullDay=0;
        }else if (Time.equals("13:30-16:00"))
        {
            isFullDay=0;
        }else if(Time.equals("9:00-16:00"))
        {
            isFullDay=1;
        }else {
        }
        int index = Time.lastIndexOf("-");
        String StartTime = Time.substring(0,index);
        String EndTime = Time.substring(index+1);
        int flag =volunteerSignService.insertVolunteerSign(IDCard,Name,StartTime,EndTime,DateDay,isFullDay,User);
        if(flag==1)
        {
            JSONObject object = new JSONObject();
            object.put("IDCardNumber",IDCard);
            object.put("Name",Name);
            object.put("Status","已签到");
            return JSON.toJSONString(object);
        }else
        {
            JSONObject object = new JSONObject();
            object.put("IDCardNumber",IDCard);
            object.put("Name",Name);
            object.put("Status","未签到");
            return JSON.toJSONString(object);
        }
    }


    @RequestMapping(value = "CertainVolunteerSignInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String CertainVolunteerSignInfo(@RequestBody String jsondata, HttpSession httpSession)
    {
        int rowCount=0;

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String StartDay = jsonObject.getString("StartDay");
        String EndDay = jsonObject.getString("EndDay");
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String Unit = jsonObject.getString("Unit");
        String Time = jsonObject.getString("Time");
        String StartTime = null;
        String EndTime = null;
        if(Time.equals("AllTimeSegment"))
        {
            StartTime = null;
            EndTime = null;
        }else
        {
            int index = Time.lastIndexOf("-");
            StartTime = Time.substring(0,index);
            EndTime = Time.substring(index+1);
        }
        JSONArray UserInfoArray = new JSONArray();
        List<VolunteerSign> volunteerSignList = volunteerSignService.getCertainVolunteerSign(IDCard,Name,StartTime,EndTime,StartDay,EndDay,Unit,-1,-1);
        try {
            rowCount = volunteerSignList.size();
            System.out.println(rowCount);
            if (rowCount%pagesize != 0) {
                rowCount = rowCount/pagesize + 1;
            } else {
                rowCount = rowCount /pagesize;
            }
            System.out.println(rowCount);
            List<VolunteerSign> volunteerSign = volunteerSignService.getCertainVolunteerSign(IDCard,Name,StartTime,EndTime,StartDay,EndDay,Unit,(page-1)*pagesize,pagesize);

            for (int i=0;i<volunteerSign.size();i++) {
                JSONObject UserInfo = new JSONObject();
                UserInfo.put("IDCardNumber", volunteerSign.get(i).getIDCardNumber());
                UserInfo.put("Name", volunteerSign.get(i).getName());
                UserInfo.put("DateDay", volunteerSign.get(i).getDateDay());
                UserInfo.put("Time", volunteerSign.get(i).getStartTime()+"-"+volunteerSign.get(i).getEndTime());
                UserInfoArray.add(UserInfo);
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }

    @RequestMapping(value = "ChangeVolunteerSignInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ChangeVolunteerSignInfo(@RequestBody String jsondata,HttpSession httpSession)
    {
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String SignDate = jsonObject.getString("SignDate");
        String OriginDate = jsonObject.getString("OriginDate");
        String SignTime = jsonObject.getString("SignTime");
        List<VolunteerSign> list = volunteerSignService.getAllVolunteerSign(IDCard,Name,null,null,OriginDate,-1,null);
        System.out.println(list.size());
        int flag =volunteerSignService.updateVolunteerSign(IDCard,SignDate,SignTime,User,OriginDate);
        if(flag==list.size())
        {
            return "true";
        }else
        {
            return "false";
        }
    }


    @RequestMapping(value = "DeleteVolunteerSignInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ChangeVolunteerSignInfo(@RequestBody String jsondata)
    {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String SignDate = jsonObject.getString("SignDate");
        String SignTime = jsonObject.getString("SignTime");
        int flag = volunteerSignService.deleteVolunteerSign(IDCard,SignDate,SignTime);
        if(flag==1)
        {
            return "true";
        }
        else
        {
            return "false";
        }
    }



    @RequestMapping(value = "VolunteerSignStatistics", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String VolunteerSignStatistics(@RequestBody String jsondata)
    {
        int rowCount=0;
        JSONObject jsonObject = JSON.parseObject(jsondata);
        int page = jsonObject.getIntValue("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String StartDate = jsonObject.getString("StartDate");
        String EndDate = jsonObject.getString("EndDate");
        String Unit = jsonObject.getString("Unit");
        List<Integer> resultFullDay = new ArrayList<>();
        List<Integer> resultNotFullDay = new ArrayList<>();
        List<String> resultName = new ArrayList<>();
        List<VolunteerSign> volunteerSignList = volunteerSignService.getCertainVolunteerSign(null,null,null,null,StartDate,EndDate,Unit,-1,-1);
        try {
            rowCount = volunteerSignList.size();
            System.out.println(rowCount);
            if (rowCount%pagesize != 0) {
                rowCount = rowCount/pagesize + 1;
            } else {
                rowCount = rowCount /pagesize;
            }
            System.out.println(rowCount);
            Map<String,Integer> map = new HashMap<>();
            Map<String,Integer> map1 = new HashMap<>();
            Map<String,Integer> map2 = new HashMap<>();
            for (int i=0;i<volunteerSignList.size();i++) {
                if(volunteerSignList.get(i).getIsFullDay()==1) {
                    if (map1.keySet().contains(volunteerSignList.get(i).getName())) {
                        map1.put(volunteerSignList.get(i).getName(), map1.get(volunteerSignList.get(i).getName()) + 1);
                    } else {
                        map1.put(volunteerSignList.get(i).getName(), 1);
                    }
                }else
                {
                    if (map2.keySet().contains(volunteerSignList.get(i).getName())) {
                        map2.put(volunteerSignList.get(i).getName(), map2.get(volunteerSignList.get(i).getName()) + 1);
                    } else {
                        map2.put(volunteerSignList.get(i).getName(), 1);
                    }

                }
                if (map.keySet().contains(volunteerSignList.get(i).getName())) {
                    map.put(volunteerSignList.get(i).getName(), map.get(volunteerSignList.get(i).getName()) + 1);
                } else {
                    map.put(volunteerSignList.get(i).getName(), 1);
                }
            }

            List<Map<String,Integer>> mapList = new ArrayList<>();
            for(String e:map.keySet())
            {
                Map<String,Integer> mapname = new HashMap<>();
                mapname.put(e,map.get(e));
                mapList.add(mapname);
            }

            Collections.sort(mapList, new Comparator<Map<String, Integer>>() {
                public int compare(Map<String, Integer> o1, Map<String, Integer> o2) {
                    return o1.get("Number").compareTo(o2.get("Number"));
                }
            });

            for (int i=(page-1)*pagesize;i<mapList.size();i++)
            {
                resultName.add(mapList.get(i).keySet().toString());
                resultFullDay.add(map1.get(mapList.get(i).keySet().toString()));
                resultNotFullDay.add(map2.get(mapList.get(i).keySet().toString()));
            }
            System.out.println(resultName);
            System.out.println(resultFullDay);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelistname",resultName);
        jsonObject1.put("pagelistFullDay",resultFullDay);
        jsonObject1.put("pagelistNotFullDay",resultNotFullDay);
        return JSON.toJSONString(jsonObject1);

    }









}
